package com.chx.three.mama.activities;

import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chx.three.mama.R;
import com.chx.three.mama.model.City;
import com.chx.three.mama.model.District;
import com.chx.three.mama.model.Province;
import com.chx.three.mama.utils.CityUtils;
import com.chx.three.mama.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

public class CityCheckActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
    List<Province> provinceList;
    List<City> cityList;
    List<District> districtList;
    List<String> data;
    ListView listView;
    RadioGroup rg;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    int index;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_check);
        initData();
        listView.setOnItemClickListener(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        listView=(ListView)findViewById(R.id.check_city_list);
        rg=(RadioGroup)findViewById(R.id.check_city_rg);
        rb1=(RadioButton)findViewById(R.id.check_city_rb1);
        rb2=(RadioButton)findViewById(R.id.check_city_rb2);
        rb3=(RadioButton)findViewById(R.id.check_city_rb3);
        rg.check(R.id.check_city_rb1);
        data=new ArrayList<>();
        provinceList= CityUtils.getPro(this);
        for (Province province : provinceList) {
            data.add(province.getName());
        }
        adapter = new ArrayAdapter<String>(this, R.layout.item_check_city,R.id.item_check_city, data);
        listView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        index++;
        if(index==1){
            int proID = provinceList.get(position).getProID();
            cityList=CityUtils.getCitys(CityCheckActivity.this,proID);
            data.clear();
            for (City city : cityList) {
                data.add(city.getName());
            }
            rb1.setText(provinceList.get(position).getName());
            adapter.notifyDataSetChanged();
        }
        if(index==2){
            int cityID = cityList.get(position).getCityID();
            districtList = CityUtils.getDistrict(CityCheckActivity.this,cityID);
            data.clear();
            for (District district : districtList) {
                data.add(district.getDisName());
            }
            adapter.notifyDataSetChanged();
        }
        if(index==3){

        }
    }
}
